package com.demo.demos.demo1.service;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.demos.demo1.mapper.PersonMapper;
import com.demo.demos.demo1.model.po.Person;
import com.demo.demos.demo1.model.query.PersonPQuery;
import com.demo.result.PageDTO;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zken
 * @since 2024/11/26 21:09:35
 */
@Service
public class PersonServiceImpl extends ServiceImpl<PersonMapper, Person> implements PersonService {


    @Override
    public PageDTO<Person> pageQuery(PersonPQuery query) {
        // 1.添加排序规则
        // 使用内置的函数转换并且 设置规则-这里为了方便扩展和使用先进行创建
        List<OrderItem> orderItems = query.toMpOrderItems();

        // 获取分页对象和排序情况
        Page<Person> page = query.toMpPageDefaultSortByIdAsc(orderItems.toArray(new OrderItem[0]));
    /*
    查询
    */
        Page<Person> p = baseMapper.selectPageVO(page, query);

        // 获取数据信息
        return PageDTO.of(p, Person.class);
    }


    @Override
    public Person savePerson(Person person) {
        person.setId(null);
        baseMapper.insert(person);
        return person;
    }

    @Override
    public Person updatePersonById(Person person) {
        baseMapper.updateById(person);
        return person;
    }

}
